package com.yoomiito.app.adapter.goods.list.sort

import android.text.SpannableString
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.*
import com.yoomiito.app.base.App
import com.yoomiito.app.model.gift.GiftGoods
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.widget.CouponView

class ThemeBaoPinGoodsAdapter(data:List<GiftGoods>?) : BaseQuickAdapter<GiftGoods, BaseViewHolder>(R.layout.item_gife_goods, data){

    override fun convert(helper: BaseViewHolder?, item: GiftGoods?) {
        helper?.getView<ImageView>(R.id.item_gift_pic).let {
            ImageLoader.getInstance().into(mContext, item?.product_img, it,R.drawable.goods_default)
        }
        helper?.setGone(R.id.goodsFlag,false)
        helper?.setText(R.id.goodsTitle,item?.product_name)
        helper?.setGone(R.id.oldGoodsPrice,false)

        //价格
        val priceT = item?.sale_price?.removeDecimalPoint()
        val price = if(item?.market_price?.isEmptyOr0()!!) "¥${priceT} " else "¥${priceT} ¥${item?.market_price.removeDecimalPoint()}"
        helper?.setText(R.id.goodsPrice,price.goodsPrice(priceT!!))

        val awardView = helper?.getView<CouponView>(R.id.awardView)
        val youmiView = helper?.getView<CouponView>(R.id.couponView)
        youmiView?.setColor(ResUtil.getColor(R.color.color_FF7D00),ResUtil.getColor(R.color.color_1AFF7D00))
        //佣金
        if (item?.yo_money!! > 0) {
            youmiView?.visibility = View.VISIBLE
            youmiView?.setText("${item.yo_money}优米")
            youmiView?.setHeaderText("送")
        } else {
            youmiView?.visibility = View.GONE
        }
        //奖金
        if (App.mAppConfig.isLogin) {
            //奖标记
            if (SpannableUtils.isEmptyOr0(item.commission)) {
                awardView?.visibility = View.GONE
            } else {//粉丝以上显示奖
                awardView?.visibility = if (UserInfoConfig.getRoleId() > 0) View.VISIBLE else View.GONE
                awardView?.setText("¥${SpannableUtils.handlePrice(item.commission)}")
            }
        } else {
            awardView?.visibility = View.GONE
        }

        //标签
        helper?.setGone(R.id.item_gift_flowLayout,false)
        helper?.setText(R.id.goodsDesTv,item!!.productPoint)
        helper?.setGone(R.id.goodsDesTv,!item!!.productPoint.isNullOrEmpty())
//        val s = ArrayList<String>()
//        if (!TextUtils.isEmpty(item?.product_point)) {
//            s.add(item!!.product_point)
//        }
//        tagFlowLayout?.adapter = object : TagAdapter<String>(s) {
//            override fun getView(parent: FlowLayout, position: Int, o: String): View {
//                val tv = LayoutInflater.from(mContext).inflate(R.layout.item_goods_flag, parent, false) as TextView
//                val lp = tv.layoutParams as ViewGroup.MarginLayoutParams
//                lp.leftMargin = 0
//                tv.text = if (o.length > 8) o.substring(0, 8) + "..." else o
//                return tv
//            }
//        }

        helper?.setText(R.id.goodsSaleSize,"月销${item.volume.toString().formatNum()}")
    }

}